package com.platform.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.dao.GoodsReadPermissionDao;
import com.platform.entity.GoodsReadPermissionEntity;

/**
 * Service实现类
 *
 * @author LarkerJin
 * @email 277901879@qq.com
 * @date 2019-07-16 17:19:59
 */
@Service("goodsReadPermissionService")
public class GoodsReadPermissionService {
    @Autowired
    private GoodsReadPermissionDao goodsReadPermissionDao;

    public GoodsReadPermissionEntity queryObject(Integer id) {
        return goodsReadPermissionDao.queryObject(id);
    }

    public List<GoodsReadPermissionEntity> queryList(Map<String, Object> map) {
        return goodsReadPermissionDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return goodsReadPermissionDao.queryTotal(map);
    }
    
    public int queryTotalByUserIdGoodsId(Integer[] goodsIds,Integer userId) {
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("goodsIds", goodsIds);
    	map.put("userId", userId);
    	map.put("expiredEndDate", new Date());
        return goodsReadPermissionDao.queryTotal(map);
    }
    
    public int isFromSendPermission(Integer goodsId,Integer goodsParentId,Integer userId,Integer userParentId) {
    	Map<String, Object> map = new HashMap<String,Object>();
    	Integer[] goodsIds = {goodsParentId,0};
    	map.put("goodsIds", goodsIds);
    	map.put("userId", userParentId);
    	map.put("expiredEndDate", new Date());
    	List<GoodsReadPermissionEntity> goodsReadPermissions = goodsReadPermissionDao.queryList(map);
    	if(goodsReadPermissions == null || goodsReadPermissions.size() == 0) {
    		return 0;
    	}
    	int canShareNum = 3;
    	for(GoodsReadPermissionEntity goodsReadPermission:goodsReadPermissions) {
    		if(goodsReadPermission.getGoodsId().equals(0)) {
    			canShareNum = 5;
    		}
    		if(goodsReadPermission.getGoodsId().equals(3)) {
    			canShareNum = 9999999;
    		}
    	}
    	
    	map.clear();
    	map.put("goodsId", goodsId);
    	map.put("source",0);
    	map.put("orderId", userParentId);
    	int shareNum = goodsReadPermissionDao.queryTotal(map);
    	if(shareNum >= canShareNum) {
    		return 0;
    	}
    	
    	GoodsReadPermissionEntity goodsReadPermission = new GoodsReadPermissionEntity();
    	goodsReadPermission.setGoodsId(goodsId);
    	goodsReadPermission.setOrderId(userParentId);
    	goodsReadPermission.setSource(0);
    	goodsReadPermission.setUserId(userId);
    	
    	return goodsReadPermissionDao.save(goodsReadPermission);
    }
        
    public int save(GoodsReadPermissionEntity goodsReadPermission) {
        return goodsReadPermissionDao.save(goodsReadPermission);
    }

    public int update(GoodsReadPermissionEntity goodsReadPermission) {
        return goodsReadPermissionDao.update(goodsReadPermission);
    }

    public int delete(Integer id) {
        return goodsReadPermissionDao.delete(id);
    }

    public int deleteBatch(Integer[] ids) {
        return goodsReadPermissionDao.deleteBatch(ids);
    }
}
